package com.elinkway.infinitemovies.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.GridView;

import com.elinkway.infinitemovies.adapter.AllTabAdapter;
import com.elinkway.infinitemovies.bean.MainHeaderBean;
import com.elinkway.infinitemovies.bigdata.BigDataApi;
import com.elinkway.infinitemovies.bigdata.BigDataConstants;
import com.elinkway.infinitemovies.ui.fragment.NewHomeFragment;
import com.elinkway.infinitemovies.ui.fragment.UtilsFragment;
import com.le123.ysdq.R;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.Map;

public class ExtraTabActivity extends BaseSecondaryActivity {
	public static final String TAG = "ExtraTabActivity";
	public static final String TAG_YIDIAN = "tag_yidian";
	private GridView mExtraTabGv;
	private ArrayList<MainHeaderBean> mHeaderList;
	private boolean isChannel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setActionBarActivityClassName(TAG);
		currentPageId = BigDataConstants.PGV_CURRENT_ID_CATEGORY_NAVIGATOR;
		setContentView(R.layout.activity_extra_tab);
		if(getIntent() != null){
			mHeaderList = (ArrayList<MainHeaderBean>) getIntent().getSerializableExtra("allTabList");
			isChannel = getIntent().getBooleanExtra(TAG_YIDIAN,false);
		}
		initActionBar();
		initView();
		setMainData();
	}

	private void setMainData() {
		if(mHeaderList != null && mHeaderList.size() > 0){
			AllTabAdapter mAllTabAdapter = new AllTabAdapter(ExtraTabActivity.this, mHeaderList);
			mExtraTabGv.setAdapter(mAllTabAdapter);
			mExtraTabGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
					if(mHeaderList.get(i) != null){
						Map<String, String> dataMap = BigDataApi.createDataMap("0", "page="+mHeaderList.get(i).getPage(),"0","-","","-");
						dataMap.put("cur_url", currentPageId);
						BigDataApi.bigDataCommonReport(dataMap, ExtraTabActivity.this);
						Intent resultIntent = new Intent();
						resultIntent.putExtra("headerBean", mHeaderList.get(i));
						setResult(NewHomeFragment.RESULT_ALL_TAB, resultIntent);
						finish();
						overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
					}
				}
			});
		}
	}

	private void initView() {
		mExtraTabGv = (GridView) findViewById(R.id.extra_tab_gv);
		if (isChannel) {
			mExtraTabGv.setVisibility(View.GONE);
			FragmentManager fManager = getSupportFragmentManager();
			FragmentTransaction transaction = fManager.beginTransaction();
			UtilsFragment utilsFragment = UtilsFragment.newInstance(2);
			transaction.add(R.id.content_channel, utilsFragment);
			transaction.commitAllowingStateLoss();
		}
	}

	@Override
	protected void initActionBar(){
		super.initActionBar();
		if (isChannel) {
			mTitleTv.setText("频道");
			mLeftBackIv.setVisibility(View.VISIBLE);
		} else {
			mTitleTv.setText("全部频道");
			mLeftBackIv.setVisibility(View.GONE);
			mRightFirstIv.setImageResource(R.drawable.right_close_normal);
			mRightFirstIv.setVisibility(View.VISIBLE);
			mRightFirstIv.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					Animation operatingAnim = AnimationUtils.loadAnimation(ExtraTabActivity.this, R.anim.center_rotate_counter);
					mRightFirstIv.startAnimation(operatingAnim);
					new Handler().postDelayed(new Runnable(){
						public void run() {
							finish();
							overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
						}
					}, 200);
				}
			});
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}
	
}
